package de.QuizCommunity.service;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import de.QuizCommunity.domain.Antwort;
import de.QuizCommunity.util.XMLGetParser;

public class Antwort_XML_Parser {

	static final String KEY_ITEM = "antwort"; // parent node
	static final String KEY_ID = "id";
	static final String KEY_ANTWORT = "antworttext";
	static final String KEY_RICHTIG = "richtig";
	static final String KEY_LONGITUDE = "longitude";
	static final String KEY_LATITUTE = "latitude";

	public static List<Antwort> getAntwort(int id) {
		String URL = "http://www.iwi.hs-karlsruhe.de/ebatc/QuizCommunity/rest/webservice/antwort/"
				+ id;
		XMLGetParser parser = new XMLGetParser();
		String xml = parser.getXmlFromUrl(URL); // getting XML
		Document doc = parser.getDomElement(xml); // getting DOM element
		List<Antwort> antwortlist = new ArrayList<Antwort>();
		NodeList nl = doc.getElementsByTagName(KEY_ITEM);
		for (int i = 0; i < nl.getLength(); i++) {
			Antwort antwort = new Antwort();
			Element e = (Element) nl.item(i);
			antwort.setAntworttext(parser.getValue(e, KEY_ANTWORT));
			antwort.setId(Integer.parseInt(parser.getValue(e, KEY_ID)));
			antwort.setRichtig(Byte.parseByte(parser.getValue(e, KEY_RICHTIG)));
			antwort.setLongitude(Double.parseDouble(parser.getValue(e,
					KEY_LONGITUDE)));
			antwort.setLatitude(Double.parseDouble(parser.getValue(e,
					KEY_LATITUTE)));
			antwortlist.add(antwort);

		}
		return antwortlist;
	}

}
